package com.xiangban.volunteer_platform.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiangban.volunteer_platform.dao.TaskMapper;
import com.xiangban.volunteer_platform.entity.BusinessUser;
import com.xiangban.volunteer_platform.entity.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author Mr. Ma
 * @Data 2021/1/21 10:17
 */
@Service
public class TaskService {

    @Autowired
    private TaskMapper taskMapper;

    public int saveTask(Task task){
        int insert = taskMapper.insert(task);
        return insert;
    }


    public int deleteTask(Task task){
        if (task.getId()==null){
            return -1;
        }
        int i = taskMapper.deleteById(task.getId());
        return i;
    }

    public int updateTask(Task task){
        Task task1 = taskMapper.selectById(task.getId());
        if (task.getId().equals(task1.getId())){
            return taskMapper.updateById(task);
        }
        return -1;
    }

    public IPage<Task> getAllTask(Integer pageNum, Integer pageSize, Task task){
        IPage<Task> page = new Page<>(pageNum == null ? 1 : pageNum,
                                        pageSize == null ? 10 :pageSize);
        LambdaQueryWrapper<Task> queryWrapper = new LambdaQueryWrapper<>();
        if (task.getName()!=null && !task.getName().isEmpty()){
            queryWrapper.like(Task::getName,task.getName());
        }
        if (task.getBusinessId()!=null && !task.getBusinessId().equals(0)){
            queryWrapper.eq(Task::getBusinessId,task.getBusinessId());
        }

        return taskMapper.selectPage(page,queryWrapper);
    }

    public Task getTaskById(Task task){
       return taskMapper.selectById(task.getId());
    }



}
